<template>
  <div>
    <el-card
        class="box-card"
        shadow="hover"
    >
      <Pagination ref="pagination" :queryPage="loadPageData">
        <div slot="search">
          <el-form :inline="true" :model="queryForm" size="mini">
            <el-form-item label="类型">
              <el-select v-model="queryForm.flowName" placeholder="请选择">
                <el-option v-for="item in flowRuleList" :key="item.name"
                           :label="item.showName"
                           :value="item.name"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="应用">
              <el-select v-model="queryForm.appName" placeholder="请选择">
                <el-option v-for="item in appList" :key="item.appName"
                           :label="item.appShowName"
                           :value="item.appName"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="申请状态">
              <el-select v-model="queryForm.applyStatus" placeholder="请选择">
                <el-option v-for="item in applyStatusList" :key="item.code"
                           :label="item.name"
                           :value="item.code"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="$refs.pagination.go(1);">查询</el-button>
              <!--              <el-button type="success" @click="$router.push('applyStepForm')">申请</el-button>-->
            </el-form-item>
          </el-form>
        </div>

        <el-table slot="table" :data="pageData" border size="mini" style="width: 100%">
          <el-table-column label="#ID" prop="id" width="80"/>
          <el-table-column :formatter="formatFlowName" label="类型" prop="flowName" width="100"/>
          <el-table-column label="申请标题" prop="applyTitle" show-overflow-tooltip/>
          <el-table-column label="申请内容" prop="remark" show-overflow-tooltip/>
          <el-table-column label="应用名称" prop="appName" width="150"/>
          <el-table-column label="状态" prop="applyStatus.name" width="100"/>
          <el-table-column label="申请时间" prop="created" width="150"/>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button size="small" type="text"
                         @click="goApplyDetail(scope.row)">查看
              </el-button>
              <el-button v-if="scope.row.applyStatus.code=='AUDITING'||scope.row.applyStatus.code=='SUBMIT'"
                         size="small"
                         type="text"
                         @click="cancelApply(scope.row.id)">取消
              </el-button>

            </template>
          </el-table-column>
        </el-table>
      </Pagination>
    </el-card>

  </div>
</template>
<script>
import {cancelApplyApi, flowRuleListApi, userApplyPageApi} from "@/api/apply";
import {appListApi} from "@/api/manage";
import {enumsApi} from "@/api/common";
import Pagination from "@/components/Pagination";

export default {
  data() {
    return {
      queryForm: {},
      pageData: [],
      flowRuleList: [],
      appList: [],
      applyStatusList: [],
      auditStatusType: {
        WAITING: "info",
        APPROVE: "success",
        REJECT: "danger",
        FINISH: "success",
        CANCEL: "warning",
      },
    };
  },
  components: {Pagination},
  methods: {
    async loadPageData(current, size) {
      let query = Object.assign(
          {
            current: current,
            size: size,
          },
          this.queryForm
      );
      let data = await userApplyPageApi(query);
      this.pageData = data.records;
      return data;
    },
    async cancelApply(applyId) {
      await cancelApplyApi(applyId);
      this.$message({
        message: `提交取消成功`,
        type: "success",
      });
      this.$refs.pagination.go(1);
    },
    goApplyDetail(apply) {
      if (apply.flowName == 'app_deploy') {
        this.$router.push({name: 'applyDetailForm', params: {applyId: apply.id}})
      }
      if (apply.flowName == 'data_deploy') {
        this.$router.push({name: 'databaseApplyForm', params: {applyId: apply.id}})
      }
    },
    formatFlowName(row) {
      return this.flowRuleList.filter(f => f.name == row.flowName).map(f => f.showName);
    }
  },
  async created() {
    this.flowRuleList = await flowRuleListApi();
    this.appList = await appListApi();
    this.applyStatusList = await enumsApi("ApplyStatus");
  },
};
</script>